<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;

class Search extends Model{
	//查询搜索表中是否有当前搜索的关键词
	public function getOne($keyword){
		$res = DB::table("keywords")->where("keyword",$keyword)->first();
		if($res == null){
			$row = 2;
		}else{
			$row = $res;
		}
		return $row;
	}
	//如果有表中有关键词就增加数量
	public function search_count($id,$data){
		$res = DB::table("keywords")->where("key_id",$id)->update($data);
		return $res;
	}
	//如果没有就添加
	public function key_add($data){
		$res = DB::table("keywords")->insert($data);
		return $res;
	}
	//查询展示商品
	public function goods_search($keyword){
		$res = DB::table("goods")->where('goods_name','like','%'.$keyword.'%')->orderBy("sort","desc")->get();
		if($res == null){
			$data = DB::table("goods")->where('goods_desc','like','%'.$keyword.'%')->orderBy("sort","desc")->get();
			if($data == null){
				$data = 2;
			}
		}else{
			$data = $res;
		}
		return $data;
	}
}